﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Framework.Bus;
using Framework.Repositories;
using Framework.Infrastructure;
using Microsoft.Practices.Unity;
using Framework.Domain;

namespace Framework.UnitOfWork
{
    public abstract class BaseUnitOfWork : IUnitOfWork
    {
        protected List<Action> _ModelContextCommitActions = new List<Action>();


        public BaseUnitOfWork(IEventBus eventBus)
        {
            _EventBus = eventBus;
        }
        #region IUnitOfWork Members

        protected IEventBus _EventBus;

        public virtual void Commit()
        {
            _EventBus.Commit();
        }

        #endregion

        #region IUnitOfWork Members

        public void RegisterModelContextCommitActions(params Action[] actions)
        {
            _ModelContextCommitActions.AddRange(actions);
        }

        #endregion


        public abstract IRepository<TAggregateRoot> GetRepository<TAggregateRoot>() where TAggregateRoot : class, IAggregateRoot;
       
        public IDomainRepository DomainRepository
        {
            get
            {
                try
                {
                    var repository = IoCFactory.Resolve<IDomainRepository>(new ParameterOverride("unitOfWork", this));
                    return repository;
                }
                catch
                {
                    throw;
                }
            }
        }
    }
}
